package com.example.parsexml;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


import java.io.InputStream;
import java.util.List;


// xml 解析方式，看日志输出
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // sax方式
        findViewById(R.id.btn_sax).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream xmlContent = getResources().openRawResource(R.raw.stu_sax);
                if(xmlContent != null) {
                    List<ModelStu> stuList = Sax.parse(xmlContent);
                    printList(stuList);
                }
            }
        });

        // pull方式
        findViewById(R.id.btn_pull).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream xmlContent = getResources().openRawResource(R.raw.stu_pull);
                if(xmlContent != null) {
                    List<ModelStu> stuList = Pull.parse(xmlContent);
                    printList(stuList);
                }
            }
        });

        // dom 方式 生成数据模型对象
        findViewById(R.id.btn_dom).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream xmlContent = getResources().openRawResource(R.raw.stu_dom);
                if(xmlContent != null) {
                    List<ModelStu> stuList = Dom.parse(xmlContent);
                    printList(stuList);
                }
            }
        });

        // dom2 方式 不生产数据模型对象
        findViewById(R.id.btn_dom2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream xmlContent = getResources().openRawResource(R.raw.stu_pull);
                if(xmlContent != null) {
                    try {
                        // TODO: 遍历
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private static void printList(List<ModelStu> list) {
        if(list.size() == 0) {
            Log.d("MainActivity", "0 size list item");
            return;
        }
        for(ModelStu objects : list) {
            Log.d("list item", objects.toString());
        }
    }

}
